package com.gpframework.module.controller.purchase;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.common.exception.ParamException;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.purchase.service.IGpPurchaseGramWeightService;
import com.gpframework.module.purchase.service.IGpPurchaseQualityFeedbackService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.purchase.domain.GpPurchaseIncomingInspect;
import com.gpframework.module.purchase.domain.vo.GpPurchaseIncomingInspectVo;
import com.gpframework.module.purchase.domain.req.GpPurchaseIncomingInspectReq;
import com.gpframework.module.purchase.domain.req.GpPurchaseIncomingInspectAdd;
import com.gpframework.module.purchase.domain.req.GpPurchaseIncomingInspectUpdate;
import com.gpframework.module.purchase.service.IGpPurchaseIncomingInspectService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
/**
 * @ClassName GpPurchaseIncomingInspectController
 * @Author gpframework
 * @Date 2024-01-09
 * @Version 1.0.0
 * @Description 采购来料检验单Controller 接口类
 */
@Validated
@Api(tags = "采购来料检验单")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/purchase/incominginspect" )
public class GpPurchaseIncomingInspectController extends BaseController {

    private final IGpPurchaseIncomingInspectService iGpPurchaseIncomingInspectService;
    private final IGpPurchaseGramWeightService iGpPurchaseGramWeightService;
    private final IGpPurchaseQualityFeedbackService iGpPurchaseQualityFeedbackService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-09
  * @Param gpPurchaseIncomingInspect
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @GetMapping("/list")
    public TableDataInfo<GpPurchaseIncomingInspectVo> list(GpPurchaseIncomingInspectReq gpPurchaseIncomingInspectReq)
    {
        startPage();
        QueryWrapper<GpPurchaseIncomingInspect> lqw = new QueryWrapper<GpPurchaseIncomingInspect>();
        gpPurchaseIncomingInspectReq.generatorQuery(lqw,true);
        List<GpPurchaseIncomingInspectVo> list = iGpPurchaseIncomingInspectService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseIncomingInspect
     * @Return AjaxResult
     */
    @ApiOperation("详情")
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpPurchaseIncomingInspectVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpPurchaseIncomingInspectService.getVoById(id));
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseIncomingInspect
     * @Return AjaxResult
     */
    @ApiOperation("新增")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpPurchaseIncomingInspectAdd gpPurchaseIncomingInspectAdd) {
        GpPurchaseIncomingInspect gpPurchaseIncomingInspect = Convert.convert(new TypeReference<GpPurchaseIncomingInspect>() {}, gpPurchaseIncomingInspectAdd);
        return toAjax(iGpPurchaseIncomingInspectService.save(gpPurchaseIncomingInspect) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseIncomingInspect
     * @Return AjaxResult
     */
    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpPurchaseIncomingInspectUpdate gpPurchaseIncomingInspectUpdate) {
        return toAjax(iGpPurchaseIncomingInspectService.editOne(gpPurchaseIncomingInspectUpdate) ? 1 : 0);
    }

    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-09
     * @Param gpPurchaseIncomingInspect
     * @Return AjaxResult
     */
    @ApiOperation("删除")
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpPurchaseIncomingInspectService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


    /**
     * @Description 生成克重表
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpIncomingInspect
     * @Return TableDataInfo
     */
    @ApiOperation("生成克重表")
    @Log(title = "生成克重表" , businessType = BusinessType.INSERT)
    @PostMapping("/addWeightMeter")
    public AjaxResult addWeightMeter(@RequestBody GpPurchaseIncomingInspectUpdate gpPurchaseIncomingInspectUpdate) {
        if (gpPurchaseIncomingInspectUpdate == null || CollectionUtils.isEmpty(gpPurchaseIncomingInspectUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpPurchaseGramWeightService.addWeightMeter(gpPurchaseIncomingInspectUpdate) ? 1 : 0);
    }

    /**
     * @Description 生成品质反馈表
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpIncomingInspect
     * @Return TableDataInfo
     */

    @ApiOperation("生成品质反馈表")
    @Log(title = "生成品质反馈表" , businessType = BusinessType.INSERT)
    @PostMapping("/addQualityfeedback")
    public AjaxResult addQualityfeedback(@RequestBody GpPurchaseIncomingInspectUpdate gpPurchaseIncomingInspectUpdate) {
        if (gpPurchaseIncomingInspectUpdate == null || CollectionUtils.isEmpty(gpPurchaseIncomingInspectUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpPurchaseQualityFeedbackService.addQualityfeedback(gpPurchaseIncomingInspectUpdate) ? 1 : 0);
    }
}
